/*---------------------------------------------------------------------------*/
// DisC Decompilation Wizard
// written by
// Satish Kumar S
// satish@miel.mot.com
//
// Copyright 1999-2001 Satish Kumar S
//
// Permission is granted to use this software for research purposes as
// long as this notice stays attached to this software.
/*---------------------------------------------------------------------------*/
#include <dos.h>
#include <dir.h>
#include "Program.h"
Program::Program()
{
  CodeSegment = 0;
  DataSegment = 0;
}
Program::~Program()
{
  if (CodeSegment) freemem(CodeSegment);
  if (DataSegment) freemem(DataSegment);
  CodeSegment = 0;
  DataSegment = 0;
}
Byte far *Program::GetCodePtr()
{
  return (Byte far *)MK_FP(CodeSegment,0);
}
Dword Program::GetCodeSize()
{
  return Size;
}
Byte far *Program::GetDataPtr()
{
  return (Byte far *)MK_FP(DataSegment,0);
}
int Program::Load(char *FileName)
{
  if (CodeSegment)
    Program::~Program();

  struct ffblk ffblk;
  if (findfirst(FileName, &ffblk, 0x17) == -1)
    return 1;
  Size = ffblk.ff_fsize;

  if (allocmem( (Size/16)+1, (unsigned *)&CodeSegment) != -1)
    return 2;

  union REGS regs;
  struct SREGS sregs;
  Word ParamBlock[2];
  ParamBlock[0] = CodeSegment;
  ParamBlock[1] = CodeSegment;

  regs.x.ax = 0x4b03;
  sregs.ds  = FP_SEG((void far *)FileName);
  regs.x.dx = FP_OFF((void far *)FileName);
  sregs.es  = FP_SEG((void far *)ParamBlock);
  regs.x.bx = FP_OFF((void far *)ParamBlock);
  int86x(0x21,&regs,&regs,&sregs);
  if (regs.x.cflag == 1)
  {
    Program::~Program();
    return 3;
  }
  return 0;
}